package com.wang.controller;

import com.wang.packpojo.ResultStatus;
import com.wang.util.DateUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class InDemo {


    @RequestMapping(value = "/inDemo")
    @CrossOrigin
    public ResultStatus inDemo() throws InterruptedException {

        ResultStatus msg = null;
        //对象锁
        Object object = new Object();
        System.out.println("开始时间：" + DateUtil.getStrDate());
        new Thread(() -> {
            synchronized (object) {


                //计数器  //小于30秒
                int i = 0;
                while (i < 100) {
                    String result = Conf.dataMap.get("001");
                    System.out.println("result:" + result);
                    System.out.println(Conf.dataMap.size());
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (result != null) {

                        object.notify();
                        break;
                    }
                    i++;
                }
                object.notify();
            }

        }).start();
        synchronized (object) {

            System.out.println("开始等待");
            object.wait(30000L);

        }


        System.out.println(Conf.map.size());
        System.out.println("-----------------");
        System.out.println("结束时间：" + DateUtil.getStrDate());
        String result = Conf.dataMap.get("001");
        if (result != null && result.equals("success")) {
            msg = new ResultStatus("success", "mqtt设置成功");
        } else {
            msg = new ResultStatus("error", "设备响应超时，请稍后查看日志信息");
        }

        Conf.dataMap.remove("001");
        return msg;

    }

}
